<template>
  <div class="comment">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      @load="loadComments"
      :finished="finished"
      finished-text="没有更多了"
      :immediate-check="false"
    >
      <div
        class="item van-hairline--bottom van-hairline--top"
        v-for="item in comments"
        :key="item.com_id"
      >
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="item.aut_photo"
        />
        <div class="info">
          <p>
            <span class="name">{{ item.aut_name }}</span>
            <span style="float: right">
              <span class="van-icon van-icon-good-job-o zan"></span>
              <span class="count">{{ item.like_count }}</span>
            </span>
          </p>
          <p>{{ item.content }}</p>
          <p>
            <span class="time">{{ item.pubdate | dateFrom }}</span
            >&nbsp;
            <van-tag plain @click="loadReply(item.com_id)"
              >{{ item.reply_count }} 回复</van-tag
            >
          </p>
        </div>
      </div>
    </van-list>
    <!-- 底部输入框 -->
    <div class="reply-container van-hairline--top">
      <van-field
        v-model="value"
        :placeholder="showReply ? '写回复...' : '写评论...'"
      >
        <van-loading v-if="submiting" slot="button" type="spinner" size="16px">
        </van-loading>
        <span class="submit" v-else slot="button" @click="submitComment"
          >提交</span
        >
      </van-field>
    </div>
    <!-- 回复 弹层-->
    <van-action-sheet v-model="showReply" class="reply_dailog" title="回复评论">
      <van-list
        v-model="reply.loading"
        :finished="reply.finished"
        finished-text="没有更多了"
      >
        <div
          class="item van-hairline--bottom van-hairline--top"
          v-for="rep in reply.list"
          :key="rep.com_id"
        >
          <van-image
            round
            width="1rem"
            height="1rem"
            fit="fill"
            :src="rep.aut_photo"
          />
          <div class="info">
            <p>
              <span class="name">{{ rep.aut_name }}</span>
            </p>
            <p>{{ rep.content }}</p>
            <p>
              <span class="time">{{ rep.pubdate | dateFrom }}</span>
            </p>
          </div>
        </div>
      </van-list>
    </van-action-sheet>
  </div>
</template>
<script>
import { getCommentsApi, addCommentsApi } from '@/api/user'
export default {
  data () {
    return {
      // 评论列表
      comments: [],
      // 评论列表加载中状态
      loading: false,
      // 评论列表完全加载完毕状态
      finished: false,
      // 输入的内容
      value: '',
      // 提交 评论或者回复 中
      submiting: false,
      offset: null,
      showReply: false,
      // 回复相关数据
      reply: {
        comid: null,
        loading: false,
        finished: false,
        // 回复列表
        list: [],
        offset: null
      }
    }
  },
  activated () {
    this.comments = []
    this.offset = null
    this.finished = false
    this.loading = true
    this.loadComments()
  },
  methods: {
    async submitComment () {
      try {
        if (this.value === '') return
        this.submiting = true
        if (!this.showReply) {
          const { data } = await addCommentsApi({ target: this.$route.params.id, content: this.value })
          this.comments.unshift(data.new_obj)
          // this.submiting = true
        } else {
          const { data } = await addCommentsApi({ target: this.reply.comid, content: this.value, art_id: this.$route.params.id })
          console.log(data)
          this.reply.list.unshift(data.new_obj)
          // this.submiting = true
        }
      } catch (error) {
      }
      this.value = ''
      this.submiting = false
    },
    async loadReply (comid) {
      this.reply.comid = comid
      this.showReply = true
      this.reply.list = []
      const { data } = await getCommentsApi({ type: 'c', source: comid, offset: this.reply.offset, limit: 10 })
      console.log(data)
      this.reply.list.push(...data.results)

      this.reply.loading = false

      if (data.last_id === data.end_id) {
        this.reply.finished = true
      } else {
        this.reply.offset = data.last_id
      }
    },
    async loadComments () {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      const { data } = await getCommentsApi({ type: 'a', source: this.$route.params.id, offset: this.offset, limit: 10 })
      this.comments.push(...data.results)

      // 加载状态结束
      this.loading = false

      // 数据全部加载完成
      if (data.last_id === data.end_id) {
        this.finished = true
      } else {
        this.offset = data.last_id
      }
    }
  }
}
</script>
<style lang="less" scoped>
.comment {
  margin-top: 10px;
  /deep/ .item {
    display: flex;
    padding: 10px 0;
    width: 100%;
    overflow: hidden;
    text-align: left;
    .info {
      flex: 1;
      padding-left: 10px;
      .name {
        color: #069;
      }
      .zan {
        vertical-align: middle;
        padding-right: 2px;
      }
      .count {
        vertical-align: middle;
        font-size: 10px;
        color: #666;
      }
      .time {
        color: #666;
      }
      p {
        padding: 5px 0;
        margin: 0;
      }
    }
  }
  /deep/ .van-button:active::before {
    background: transparent;
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 44px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
  .submit {
    font-size: 12px;
    color: #3296fa;
  }
}
</style>
